home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / patch / malloc.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  12KB  |  473 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6. /*
  7.  * @(#)nmalloc.c 1 (Caltech) 2/21/82
  8.  *
  9.  *    U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
  10.  *
  11.  *    Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
  12.  *
  13.  * This is a very fast storage allocator.  It allocates blocks of a small 
  14.  * number of different sizes, and keeps free lists of each size.  Blocks
  15.  * that don't exactly fit are passed up to the next larger size.  In this 
  16.  * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
  17.  * This is designed for use in a program that uses vast quantities of
  18.  * memory, but bombs when it runs out.  To make it a little better, it
  19.  * warns the user when he starts to get near the end.
  20.  *
  21.  * June 84, ACT: modified rcheck code to check the range given to malloc,
  22.  * rather than the range determined by the 2-power used.
  23.  *
  24.  * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
  25.  * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
  26.  * You should call malloc_init to reinitialize after loading dumped Emacs.
  27.  * Call malloc_stats to get info on memory stats if MSTATS turned on.
  28.  * realloc knows how to return same block given, just changing its size,
  29.  * if the power of 2 is correct.
  30.  */
  31.  
  32. /*
  33.  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
  34.  * smallest allocatable block is 8 bytes.  The overhead information will
  35.  * go in the first int of the block, and the returned pointer will point
  36.  * to the second.
  37.  *
  38. #ifdef MSTATS
  39.  * nmalloc[i] is the difference between the number of mallocs and frees
  40.  * for a given block size.
  41. #endif /* MSTATS */
  42.  */
  43.  
  44. #define ISALLOC ((char) 0xf7)    /* magic byte that implies allocation */
  45. #define ISFREE ((char) 0x54)    /* magic byte that implies free block */
  46.                 /* this is for error checking only */
  47.  
  48. extern char etext;
  49.  
  50. /* end of the program; can be changed by calling init_malloc */
  51. static char *endofpure = &etext;
  52.  
  53. #ifdef MSTATS
  54. static int nmalloc[30];
  55. static int nmal, nfre;
  56. #endif /* MSTATS */
  57.  
  58. /* If range checking is not turned on, all we have is a flag indicating
  59.    whether memory is allocated, an index in nextf[], and a size field; to
  60.    realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
  61.    on whether the former can hold the exact size (given the value of
  62.    'index').  If range checking is on, we always need to know how much space
  63.    is allocated, so the 'size' field is never used. */
  64.  
  65. struct mhead {
  66.     char     mh_alloc;    /* ISALLOC or ISFREE */
  67.     char     mh_index;    /* index in nextf[] */
  68. /* Remainder are valid only when block is allocated */
  69.     unsigned short mh_size;    /* size, if < 0x10000 */
  70. #ifdef rcheck
  71.     unsigned mh_nbytes;    /* number of bytes allocated */
  72.     int      mh_magic4;    /* should be == MAGIC4 */
  73. #endif /* rcheck */
  74.     };
  75.  
  76. /* Access free-list pointer of a block.
  77.   It is stored at block + 4.
  78.   This is not a field in the mhead structure
  79.   because we want sizeof (struct mhead)
  80.   to describe the overhead for when the block is in use,
  81.   and we do not want the free-list pointer to count in that.  */
  82.  
  83. #define CHAIN(a) \
  84.   (*(struct mhead **) (sizeof (char *) + (char *) (a)))
  85.  
  86. #ifdef rcheck
  87.  
  88. /* To implement range checking, we write magic values in at the beginning and
  89.    end of each allocated block, and make sure they are undisturbed whenever a
  90.    free or a realloc occurs. */
  91. /* Written in each of the 4 bytes following the block's real space */
  92. #define MAGIC1 0x55
  93. /* Written in the 4 bytes before the block's real space */
  94. #define MAGIC4 0x55555555
  95. #define ASSERT(p) if (!(p)) botch("p"); else
  96. static
  97. botch(s)
  98.     char *s;
  99. {
  100.  
  101.     printf("assertion botched: %s\n", s);
  102.     abort();
  103. }
  104. #define EXTRA  4        /* 4 bytes extra for MAGIC1s */
  105. #else
  106. #define ASSERT(p)
  107. #define EXTRA  0
  108. #endif /* rcheck */
  109.  
  110. /* nextf[i] is free list of blocks of size 2**(i + 3)  */
  111.  
  112. static struct mhead *nextf[30];
  113.  
  114. #ifdef    M_WARN
  115. /* Number of bytes of writable memory we can expect to be able to get */
  116. static int  lim_data;
  117. /* Level number of warnings already issued.
  118.   0 -- no warnings issued.
  119.   1 -- 75% warning already issued.
  120.   2 -- 85% warning already issued.
  121. */
  122. static int  warnlevel;
  123. #endif /* M_WARN */
  124.  
  125. /* nonzero once initial bunch of free blocks made */
  126. static int gotpool;
  127.  
  128. /* Cause reinitialization based on job parameters;
  129.   also declare where the end of pure storage is. */
  130. malloc_init (end)
  131.     char *end; {
  132.     endofpure = end;
  133. #ifdef    M_WARN
  134.     lim_data = 0;
  135.     warnlevel = 0;
  136. #endif /* M_WARN */
  137.     }
  138.  
  139. static
  140. morecore (nu)            /* ask system for more memory */
  141.     register int nu; {        /* size index to get more of  */
  142.     char   *sbrk ();
  143.     register char  *cp;
  144.     register int    nblks;
  145.     register int    siz;
  146.  
  147. #ifdef    M_WARN
  148. #ifndef BSD42
  149. #ifdef USG
  150.     extern long ulimit ();
  151.     if (lim_data == 0)        /* find out how much we can get */
  152.         lim_data = ulimit (3, 0) - TEXT_START;
  153. #else    /*HMS: was endif */
  154.     if (lim_data == 0)        /* find out how much we can get */
  155.         lim_data = vlimit (LIM_DATA, -1);
  156. #endif /* USG */    /HMS:* was not here */
  157. #else
  158.     if (lim_data == 0) {
  159.         struct rlimit   XXrlimit;
  160.  
  161.         getrlimit (RLIMIT_DATA, &XXrlimit);
  162.         lim_data = XXrlimit.rlim_cur;}    /* soft limit */
  163. #endif /* BSD42 */
  164. #endif /* M_WARN */
  165.  
  166.     /* On initial startup, get two blocks of each size up to 1k bytes */
  167.     if (!gotpool)
  168.         getpool (), getpool (), gotpool = 1;
  169.  
  170.     /* Find current end of memory and issue warning if getting near max */
  171.  
  172.     cp = sbrk (0);
  173.     siz = cp - endofpure;
  174. #ifdef    M_WARN
  175.     switch (warnlevel) {
  176.         case 0: 
  177.         if (siz > (lim_data / 4) * 3) {
  178.             warnlevel++;
  179.             malloc_warning ("Warning: past 75% of memory limit");}
  180.         break;
  181.         case 1: 
  182.         if (siz > (lim_data / 20) * 17) {
  183.             warnlevel++;
  184.             malloc_warning ("Warning: past 85% of memory limit");}
  185.         break;
  186.         case 2: 
  187.         if (siz > (lim_data / 20) * 19) {
  188.             warnlevel++;
  189.             malloc_warning ("Warning: past 95% of memory limit");}
  190.         break;}
  191. #endif /* M_WARN */
  192.  
  193.     if ((int) cp & 0x3ff)    /* land on 1K boundaries */
  194.         sbrk (1024 - ((int) cp & 0x3ff));
  195.  
  196.     /* Take at least 2k, and figure out how many blocks of the desired size we're about to get */
  197.     nblks = 1;
  198.     if ((siz = nu) < 8)
  199.         nblks = 1 << ((siz = 8) - nu);
  200.  
  201.     if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
  202.         return;            /* no more room! */
  203.     if ((int) cp & 7) {        /* shouldn't happen, but just in case */
  204.         cp = (char *) (((int) cp + 8) & ~7);
  205.         nblks--;}
  206.  
  207.     /* save new header and link the nblks blocks together */
  208.     nextf[nu] = (struct mhead *) cp;
  209.     siz = 1 << (nu + 3);
  210.     while (1) {
  211.         ((struct mhead *) cp) -> mh_alloc = ISFREE;
  212.         ((struct mhead *) cp) -> mh_index = nu;
  213.         if (--nblks <= 0) break;
  214.         CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
  215.         cp += siz;}
  216. /*    CHAIN ((struct mhead *) cp) = 0;    /* since sbrk() returns cleared core, this is already set */
  217.     }
  218.  
  219. static
  220. getpool () {
  221.     register int nu;
  222.     register char *cp = sbrk (0);
  223.  
  224.     if ((int) cp & 0x3ff)    /* land on 1K boundaries */
  225.         sbrk (1024 - ((int) cp & 0x3ff));
  226.  
  227.     /* Get 2k of storage */
  228.  
  229.     cp = sbrk (04000);
  230.     if (cp == (char *) -1)
  231.         return;
  232.  
  233.     /* Divide it into an initial 8-word block
  234.     plus one block of size 2**nu for nu = 3 ... 10.  */
  235.  
  236.     CHAIN (cp) = nextf[0];
  237.     nextf[0] = (struct mhead *) cp;
  238.     ((struct mhead *) cp) -> mh_alloc = ISFREE;
  239.     ((struct mhead *) cp) -> mh_index = 0;
  240.     cp += 8;
  241.  
  242.     for (nu = 0; nu < 7; nu++) {
  243.         CHAIN (cp) = nextf[nu];
  244.         nextf[nu] = (struct mhead *) cp;
  245.         ((struct mhead *) cp) -> mh_alloc = ISFREE;
  246.         ((struct mhead *) cp) -> mh_index = nu;
  247.         cp += 8 << nu;}}
  248.  
  249. char *
  250. malloc (n)        /* get a block */
  251.     unsigned n; {
  252.     register struct  mhead *p;
  253.     register unsigned int  nbytes;
  254.     register int    nunits = 0;
  255.  
  256.     /* Figure out how many bytes are required, rounding up to the nearest
  257.     multiple of 4, then figure out which nextf[] area to use */
  258.     nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
  259.         {
  260.         register unsigned int   shiftr = (nbytes - 1) >> 2;
  261.  
  262.         while (shiftr >>= 1)
  263.             nunits++;
  264.         }
  265.  
  266.     /* If there are no blocks of the appropriate size, go get some */
  267.     /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
  268.     if (nextf[nunits] == 0)
  269.         morecore (nunits);
  270.  
  271.     /* Get one block off the list, and set the new list head */
  272.     if ((p = nextf[nunits]) == 0)
  273.         return 0;
  274.     nextf[nunits] = CHAIN (p);
  275.  
  276.     /* Check for free block clobbered */
  277.     /* If not for this check, we would gobble a clobbered free chain ptr */
  278.     /* and bomb out on the NEXT allocate of this size block */
  279.     if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
  280. #ifdef rcheck
  281.         botch ("block on free list clobbered");
  282. #else
  283.         abort ();
  284. #endif /* rcheck */
  285.  
  286.     /* Fill in the info, and if range checking, set up the magic numbers */
  287.     p -> mh_alloc = ISALLOC;
  288. #ifdef rcheck
  289.     p -> mh_nbytes = n;
  290.     p -> mh_magic4 = MAGIC4;
  291.         {
  292.         register char  *m = (char *) (p + 1) + n;
  293.  
  294.         *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
  295.         }
  296. #else
  297.     p -> mh_size = n;
  298. #endif /* rcheck */
  299. #ifdef MSTATS
  300.     nmalloc[nunits]++;
  301.     nmal++;
  302. #endif /* MSTATS */
  303.     return (char *) (p + 1);}
  304.  
  305. free (mem)
  306.     char *mem; {
  307.     register struct mhead *p;
  308.         {
  309.         register char *ap = mem;
  310.  
  311.         ASSERT (ap != 0);
  312.         p = (struct mhead *) ap - 1;
  313.         ASSERT (p -> mh_alloc == ISALLOC);
  314. #ifdef rcheck
  315.         ASSERT (p -> mh_magic4 == MAGIC4);
  316.         ap += p -> mh_nbytes;
  317.         ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
  318.         ASSERT (*ap++ == MAGIC1); ASSERT (*ap   == MAGIC1);
  319. #endif /* rcheck */
  320.         }
  321.         {
  322.         register int nunits = p -> mh_index;
  323.  
  324.         ASSERT (nunits <= 29);
  325.         p -> mh_alloc = ISFREE;
  326.         CHAIN (p) = nextf[nunits];
  327.         nextf[nunits] = p;
  328. #ifdef MSTATS
  329.         nmalloc[nunits]--;
  330.         nfre++;
  331. #endif /* MSTATS */
  332.         }
  333.     }
  334.  
  335. char *
  336. realloc (mem, n)
  337.     char *mem;
  338.     register unsigned n; {
  339.     register struct mhead *p;
  340.     register unsigned int tocopy;
  341.     register int nbytes;
  342.     register int nunits;
  343.  
  344.     if ((p = (struct mhead *) mem) == 0)
  345.         return malloc (n);
  346.     p--;
  347.     nunits = p -> mh_index;
  348.     ASSERT (p -> mh_alloc == ISALLOC);
  349. #ifdef rcheck
  350.     ASSERT (p -> mh_magic4 == MAGIC4);
  351.         {
  352.         register char *m = mem + (tocopy = p -> mh_nbytes);
  353.         ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
  354.         ASSERT (*m++ == MAGIC1); ASSERT (*m   == MAGIC1);
  355.         }
  356. #else
  357.     if (p -> mh_index >= 13)
  358.         tocopy = (1 << (p -> mh_index + 3)) - sizeof *p;
  359.     else
  360.         tocopy = p -> mh_size;
  361. #endif /* rcheck */
  362.  
  363.     /* See if desired size rounds to same power of 2 as actual size. */
  364.     nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
  365.  
  366.     /* If ok, use the same block, just marking its size as changed.  */
  367.     if (nbytes > (4 << nunits) && nbytes <= (8 << nunits)) {
  368. #ifdef rcheck
  369.         register char *m = mem + tocopy;
  370.         *m++ = 0;  *m++ = 0;  *m++ = 0;  *m++ = 0;
  371.         p-> mh_nbytes = n;
  372.         m = mem + n;
  373.         *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;
  374. #else
  375.         p -> mh_size = n;
  376. #endif /* rcheck */
  377.         return mem;}
  378.  
  379.     if (n < tocopy)
  380.         tocopy = n;
  381.         {
  382.         register char *new;
  383.         void bcopy();    /*HMS: here? */
  384.  
  385.         if ((new = malloc (n)) == 0)
  386.             return 0;
  387.         bcopy (mem, new, tocopy);
  388.         free (mem);
  389.         return new;
  390.         }
  391.     }
  392.  
  393. #ifdef MSTATS
  394. /* Return statistics describing allocation of blocks of size 2**n. */
  395.  
  396. struct mstats_value {
  397.     int blocksize;
  398.     int nfree;
  399.     int nused;
  400.     };
  401.  
  402. struct mstats_value
  403. malloc_stats (size)
  404.     int size; {
  405.     struct mstats_value v;
  406.     register int i;
  407.     register struct mhead *p;
  408.  
  409.     v.nfree = 0;
  410.  
  411.     if (size < 0 || size >= 30) {
  412.         v.blocksize = 0;
  413.         v.nused = 0;
  414.         return v;}
  415.  
  416.     v.blocksize = 1 << (size + 3);
  417.     v.nused = nmalloc[size];
  418.  
  419.     for (p = nextf[size]; p; p = CHAIN (p))
  420.         v.nfree++;
  421.  
  422.     return v;}
  423. #endif
  424.  
  425. /* how much space is available? */
  426.  
  427. unsigned freespace() {
  428.       register int i, j;
  429.       register struct mhead *p;
  430.       register unsigned space = 0;
  431.     int local;    /* address only is used */
  432.  
  433.     space = (char *)&local - sbrk(0);    /* stack space */
  434.  
  435.       for (i = 0; i < 30; i++) {
  436.           for (j = 0, p = nextf[i]; p; p = CHAIN (p), j++) ;
  437.           space += j * (1 << (i + 3));}
  438.  
  439.     return(space);}
  440.  
  441. /* How big is this cell? */
  442.  
  443. unsigned mc_size(cp)
  444.     char *cp;{
  445.     register struct mhead *p;
  446.  
  447.     if ((p = (struct mhead *) cp) == 0) {
  448.         /*HMS? */
  449.         }
  450.     p--;
  451. #ifdef rcheck
  452.     return p -> mh_nbytes;
  453. #else
  454.     return (1 << (p -> mh_index + 3)) - sizeof *p;
  455. /**/
  456. /*    if (p -> mh_index >= 13)
  457. /*        return (1 << (p -> mh_index + 3)) - sizeof *p;
  458. /*    else
  459. /*        return p -> mh_size;
  460. /**/
  461. #endif /* rcheck */
  462.     }
  463.  
  464. /*HMS: Really should use memcpy, if available... */
  465.  
  466. void bcopy(source, dest, len)
  467.     register char *source, *dest;
  468.     register len; {
  469.     register i;
  470.     
  471.     for (i = 0; i < len; i++)
  472.         *dest++ = *source++;}
  473.